﻿#include "../lv_Agriculture.h"

static void lv_Mqtt_Content(lv_obj_t *parent);
static void lv_Mqtt_inputCard(lv_obj_t *parent, const char *title);
static void gotoMqttSend_Callback(lv_event_t *e);
void lv_MqttExit_Btn_Callback(lv_event_t *e);
void lv_Mqtt_Exit_Btn(lv_obj_t *parent);


lv_obj_t *lv_MqttConfig_Screen;
lv_group_t *mqttconfig_group;

void lv_MqttConfig_Page(void)
{
    lv_MqttConfig_Screen = create_srceen();
    lv_Top_StateBar(lv_MqttConfig_Screen); // 顶部状态栏
    lv_Mqtt_Content(lv_MqttConfig_Screen); // 内容区
    lv_Mqtt_Exit_Btn(lv_MqttConfig_Screen); // 返回按钮

    lv_obj_t *gotoMqttSend_Btn = create_btn(lv_MqttConfig_Screen,LV_SYMBOL_RIGHT);
    lv_obj_set_align(gotoMqttSend_Btn, LV_ALIGN_BOTTOM_RIGHT);
    lv_obj_add_event_cb(gotoMqttSend_Btn, gotoMqttSend_Callback, LV_EVENT_PRESSED, NULL);
    lv_group_add_obj(mqttconfig_group,gotoMqttSend_Btn);
}


static void lv_Mqtt_Content(lv_obj_t *parent)
{
    lv_obj_t *Content = lv_obj_create(parent);
    lv_obj_add_style(Content, &lv_Clean_Style, LV_STATE_DEFAULT);
    lv_obj_set_flex_flow(Content, LV_FLEX_FLOW_ROW_WRAP);
    lv_obj_set_align(Content, LV_ALIGN_BOTTOM_MID);
    lv_obj_set_size(Content, lv_pct(100), lv_pct(85));

    mqttconfig_group =  lv_group_create();
    lv_indev_set_group(indev_keypad, mqttconfig_group);

    lv_Mqtt_inputCard(Content, "ServerAddress:");
    lv_Mqtt_inputCard(Content, "Name:");
    lv_Mqtt_inputCard(Content, "ClientID:");
    lv_Mqtt_inputCard(Content, "UserName:");
    lv_Mqtt_inputCard(Content, "PassWord:");
}

static void lv_Mqtt_inputCard(lv_obj_t *parent, const char *title)
{
    lv_obj_t *Card = lv_obj_create(parent);
    lv_obj_set_size(Card, lv_pct(49), lv_pct(45));

    lv_obj_t *Title_Label = lv_label_create(Card);
    lv_obj_set_align(Title_Label, LV_ALIGN_TOP_LEFT);
    lv_obj_set_size(Title_Label, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
    lv_label_set_text(Title_Label, title);

    lv_obj_t *Input = lv_textarea_create(Card);
    lv_obj_set_align(Input, LV_ALIGN_BOTTOM_LEFT);
    lv_obj_set_size(Input, lv_pct(90), LV_SIZE_CONTENT);
    lv_textarea_set_one_line(Input, true);

    lv_obj_t *KeyBorad = lv_keyboard_create(lv_layer_top());
    lv_obj_add_flag(KeyBorad, LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_event_cb(Input, KeyBorad_Callback, LV_EVENT_ALL, KeyBorad);
    
    lv_group_add_obj(mqttconfig_group, Input);
    lv_group_add_obj(mqttconfig_group,KeyBorad);
}

void lv_Mqtt_Exit_Btn(lv_obj_t *parent)
{
    lv_obj_t *Exit_Btn = lv_btn_create(parent);
    lv_obj_t *Exit_Label = lv_label_create(Exit_Btn);
    lv_label_set_text(Exit_Label, LV_SYMBOL_LEFT);
    lv_obj_set_align(Exit_Btn, LV_ALIGN_BOTTOM_LEFT);
    lv_obj_add_event_cb(Exit_Btn, lv_MqttExit_Btn_Callback, LV_EVENT_ALL, NULL);
    lv_group_add_obj(mqttconfig_group, Exit_Btn);
}

void lv_MqttExit_Btn_Callback(lv_event_t *e)
{
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t *target = lv_event_get_target(e);
    if (event_code == LV_EVENT_CLICKED)
    {
        lv_Home_Page();
        lv_scr_load_anim(lv_Home_Screen, LV_SCR_LOAD_ANIM_FADE_ON, 500, 0, false);
    }
}

static void gotoMqttSend_Callback(lv_event_t *e)
{
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t *target = lv_event_get_target(e);
    lv_obj_t *data = lv_event_get_user_data(e);
    if (event_code == LV_EVENT_PRESSED)
    {
        lv_MqttSend_Page();
        lv_scr_load_anim(lv_MqttSend_Screen, LV_SCR_LOAD_ANIM_FADE_ON, 500, 0, false);
    }
}